package com.lemon.tieba.post.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lemon.common.service.impl.BaseServiceImpl;
import com.lemon.tieba.post.domain.Post;
import com.lemon.tieba.post.domain.dto.Query;
import com.lemon.tieba.post.mapper.PostMapper;
import com.lemon.tieba.post.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * 帖子 ServiceImpl
 *
 * @author ymh
 * @date 2019-3-29 16:40
 */
@Service
public class PostServiceImpl extends BaseServiceImpl<Post> implements PostService {

    @Autowired
    private PostMapper postMapper;

    /**
     * 新增
     *
     * @param post
     * @return
     */
    @Override
    public int save(Post post) {
        int count = 0;
        // 设置index
        if (post.getParentId() != null) {
            int index = countByParentId(post.getParentId()) + 1;
            post.setIndex(index);
        }
        count = insert(post);

        if (post.getType() == Post.TYPE_POST) { // 如果保存的是帖子
            Post floor = new Post();
            floor.setUserId(post.getUserId());
            floor.setUsername(post.getUsername());
            floor.setAvator(post.getAvator());
            floor.setParentId(post.getId());
            floor.setContent(post.getContent());
            floor.setType(Post.TYPE_FLOOR);
            floor.setIndex(1);
            insert(floor);
        }

        return count;
    }

    /**
     * 查询帖子列表
     *
     * @param query
     * @return
     */
    @Override
    public PageInfo<Post> listPost(Query query) {
        // 容错处理
        if (query.getPageNum() == null) query.setPageNum(1);
        if (query.getPageSize() == null) query.setPageSize(10);

        Post post = new Post();
        post.setType(Post.TYPE_POST);
        return selectPage(query.getPageNum(), query.getPageSize(), post);
    }

    /**
     * 查询帖子楼层列表
     *
     * @param query
     * @return
     */
    @Override
    public PageInfo<Post> listFloor(Query query) {
        // 容错处理
        if (query.getPageNum() == null) query.setPageNum(1);
        if (query.getPageSize() == null) query.setPageSize(10);

        // 分页
        PageHelper.startPage(query.getPageNum(), query.getPageSize());

        // 设置查询条件
        Example example = new Example(Post.class);
        Example.Criteria criteria = example.createCriteria();
        if (query.getParentId() != null) {
            criteria.andEqualTo("parentId", query.getParentId());
        }
        criteria.andEqualTo("type", Post.TYPE_FLOOR);

        // 查询
        List<Post> posts = postMapper.selectByExample(example);

        // 查询回复
        for (Post post : posts) {
            Query replyQuery = new Query();
            replyQuery.setParentId(post.getId());
            PageInfo<Post> replyPageInfo = listReply(replyQuery);
            post.setChildren(replyPageInfo);
        }

        return new PageInfo(posts);
    }

    /**
     * 查询回复列表
     *
     * @param query
     * @return
     */
    @Override
    public PageInfo<Post> listReply(Query query) {
        // 容错处理
        if (query.getPageNum() == null) query.setPageNum(1);
        if (query.getPageSize() == null) query.setPageSize(5);

        // 分页
        PageHelper.startPage(query.getPageNum(), query.getPageSize());

        // 设置查询条件
        Example example = new Example(Post.class);
        Example.Criteria criteria = example.createCriteria();
        if (query.getParentId() != null) {
            criteria.andEqualTo("parentId", query.getParentId());
        }
        criteria.andEqualTo("type", Post.TYPE_REPLY);
        example.setOrderByClause("id desc"); // 时间倒序

        // 查询
        List<Post> posts = postMapper.selectByExample(example);
        return new PageInfo(posts);
    }

    /**
     * 根据 parentId 统计数据条数
     *
     * @param parentId
     * @return
     */
    @Override
    public int countByParentId(Long parentId) {
        Post post = new Post();
        post.setParentId(parentId);
        return postMapper.selectCount(post);
    }
}
